/*
 * Copyright (C) 2017 wuys Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include "sys_log.h"
#include "hal_socket.h"

#define CONFIG_LOG_COLOR_ENABLE
#ifdef CONFIG_LOG_COLOR_ENABLE
static const char *sys_log_string[] = {
    "\033[1m\033[40;31mE\033[0m", /*LOG_ERR*/
    "\033[1m\033[40;34mW\033[0m", /*LOG_WARNING*/
    "\033[1m\033[40;35mI\033[0m", /*LOG_INFO*/
    "\033[1m\033[40;33mD\033[0m"  /*LOG_DEBUG*/
};
#else
static const char *sys_log_string[] = {
    "E", /*LOG_ERR*/
    "W", /*LOG_WARNING*/
    "I", /*LOG_INFO*/
    "D"  /*LOG_DEBUG*/
};
#endif
static char g_log_level = SYS_LOG_LEVEL_DEBUG + 1; // 默认使能全部日志打印

uint32_t HAL_OsGetTick(void) { return 0; }
#define TAIL_LOG "\r\n"
char sys_log_get_enable(void);
int sys_log_core(const char *module, int info, int line, const char *fmt, ...)
{
    va_list args;
    static char trans_buf[2048];
    memset(trans_buf, 0, sizeof(trans_buf));
    if (info < (g_log_level))
    {
        snprintf(trans_buf, sizeof(trans_buf), "[%s][%d]", module, line);
        /* printf args */
        va_start(args, fmt);
        vsnprintf(trans_buf + strlen(trans_buf), sizeof(trans_buf) - strlen(trans_buf), fmt, args);
        va_end(args);
        // snprintf(trans_buf+strlen(trans_buf),sizeof(trans_buf)-strlen(trans_buf),"\r\n");
        // xil_printf("\r\nroot:/$ %s",trans_buf);

#ifdef UASRT1_IS_ENABLE
        hal_UsartSendBuffer((uint8_t *)trans_buf, strlen(trans_buf), USART_TYPE_1);
#endif
    }
    return 0;
}

int show_xxx_port_great(char *s, uint8_t *buf, int size, char *mode)
{
    if (s)
        SLOG_STDOUT("%s,%d,", s, size);
    int i = 0;
    for (; i < size; i++)
    {
        SLOG_STDOUT(mode, buf[i]);
    }
    SLOG_STDOUT("\r\n");
    return 0;
}

int sys_log_get_level(void) { return g_log_level; }

int sys_log_set_level(int level)
{
    g_log_level = level;
    return g_log_level;
}